package jiyun.com.yiyanmeng;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import base.BaseActivity;
import bean.RegBean;
import presenter.RegPresenter;
import utils.ToastUtil;
import view.RegView;

public class RegActivity extends BaseActivity<RegView, RegPresenter> implements RegView,View.OnClickListener{


    private ImageView back;
    private EditText edPhone;
    private EditText edCode;
    private TextView tvCode;
    private EditText edPassword;
    private TextView btn;
    private CheckBox check;
    private TextView tv;
    private int myCode;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            tvCode.setText(what+"秒后重新获取");
            if (what==0){
                timer.cancel();
                tvCode.setClickable(true);
                tvCode.setText("获取验证码");

            }


        }
    };
    private int time;
    private Timer timer;


    @Override
    public void showToast(String msg) {
        ToastUtil.show(this,msg);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {
        tv.setOnClickListener(this);
        tvCode.setOnClickListener(this);
        back.setOnClickListener(this);
        btn.setOnClickListener(this);

    }

    @Override
    protected void initView() {
        back = findViewById(R.id.iv_back);
        edPhone = findViewById(R.id.regis_phone);
        edCode = findViewById(R.id.regis_code);
        tvCode = findViewById(R.id.regis_codes);
        edPassword = findViewById(R.id.et_pass);
        btn = findViewById(R.id.regis_submit);
        check = findViewById(R.id.regis_check);
        tv = findViewById(R.id.reg_tv);
        check.setChecked(true);

    }

    @Override
    protected RegPresenter createPresenter() {
        return new RegPresenter();
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_reg;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reg_tv:
                //跳转条款内容
                Intent intent = new Intent(this, TiaoKuanActivity.class);
                startActivity(intent);

                break;
            case R.id.regis_submit:
                //注册提交
                String phone = edPhone.getText().toString().trim();
                String password = edPassword.getText().toString().trim();
                String codeStr = edCode.getText().toString().trim();
                int code=0;
                if (!TextUtils.isEmpty(codeStr)){
                     code = Integer.parseInt(codeStr);
                }

                boolean checked = check.isChecked();
                if (TextUtils.isEmpty(phone)){
                    ToastUtil.show(this,"手机号不能为空！");
                    return;
                }
                if (TextUtils.isEmpty(password)){
                    ToastUtil.show(this,"密码不能为空！");
                    return;
                }
                if (password.length()>12||password.length()<6){
                    ToastUtil.show(this,"密码必须为6-12位！");
                    return;

                }
                if (myCode==0||myCode!=code){
                    ToastUtil.show(this,"验证码不正确！");
                    return;
                }
                if (!checked){
                    ToastUtil.show(this,"请仔细阅读条款内容并确认！");
                    return;

                }

                mPresenter.reg(phone,password,code);

                break;
            case R.id.regis_codes:
                //申请验证码
                String phoneNum = edPhone.getText().toString().trim();
                if (!TextUtils.isEmpty(phoneNum)){
                    mPresenter.requestCode(phoneNum);

                }else {
                    ToastUtil.show(this,"手机号不能为空！");
                }


                break;
            case R.id.iv_back:
                //关闭本页面
                finish();
                break;

        }

    }

    @Override
    public void onCodeSuccess(int code) {
        time = 60;
        myCode = code;
        tvCode.setClickable(false);
        //创建一个定时器60秒后才能继续发送验证码
        timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                time--;
                Message obtain = Message.obtain();
                obtain.what= time;
                handler.sendMessage(obtain);

            }
        };
        timer.schedule(timerTask,1000,1000);


    }

    @Override
    public void onRegSuccess(RegBean regBean) {
        //关闭本页面
        finish();

    }
}
